package com.avit.tv.kui.base;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.avit.tv.kui.background.BgDrawable;
import com.avit.tv.kui.utils.Utils;

/**
 * 把持焦点的layout
 * @author like
 * @date 2020/6/12
 */
public abstract class BaseFocusLayout extends FrameLayout {

    protected final int DEFAULT_SIZE ;
    protected final int DEFAULT_TITLE_SIZE ;
    protected final int DEFAULT_BUTTON_SIZE ;

    protected final int DEFAULT_RESOURCE = -0x0001;

    private boolean isLockFocus;

    public BaseFocusLayout(@NonNull Context context) {
        this(context, null);
    }

    public BaseFocusLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseFocusLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        int radix = Utils.getRadix(context);
        DEFAULT_SIZE = 10*radix;
        DEFAULT_TITLE_SIZE = 15*radix;
        DEFAULT_BUTTON_SIZE = 2*DEFAULT_SIZE;
        if (getChildCount()>0)
            removeAllViews();
        initView(context,attrs,defStyleAttr);
        initContent(context);
        insertView();
        initFocus();
    }

    /**
     * 设置可获取焦点属性
     */
    protected void initFocus(){
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    /**
     * 锁焦点
     */
    public void lockFocus(){
        requestFocus();
        isLockFocus = true;
    }

    @Override
    public boolean isFocused() {
        return isLockFocus || super.isFocused();
    }

    /**
     * 失焦
     * @param isforever 是否永久失去焦点
     */
    public void loseFocus(boolean isforever){
        if (isforever){
            setFocusable(false);
            setFocusableInTouchMode(false);
        }
        clearFocus();
        if (getChildCount()>0){
            for (int i =0 ; i< getChildCount() ; i ++){
                View childView = getChildAt(i);
                clearChildFocus(childView);
            }
        }
    }

    /**
     * 获取view 的哈希值，用于辨别当前view
     * @param view
     * @return
     */
    protected String getHashViewCode(View view){
        return Integer.toHexString(System.identityHashCode(view));
    }

    /**
     * 设置自定义的背景
     * @param background
     */
    public void setBackground(BgDrawable background) {
        if (background!=null)
        background.targetDrawable(this);
    }

    /**
     * 设置TextView 的size
     * @param textView
     * @param textSize
     */
    protected void setTextSize(TextView textView, int textSize){
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX , textSize);
    }

    /**
     * 获取属性
     * @param context
     */
    protected abstract void initView(Context context,AttributeSet attrs , int defStyleAttr);

    /**
     * 新建view
     * @param context
     */
    protected abstract void initContent(Context context);

    /**
     * 填充layout
     */
    protected abstract void insertView();

}
